﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuatToanDAO;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class Admin_UploadSourceCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if ((Session["UserName"] == null))
        {
            Response.Redirect("Default.aspx?path=" + Request.Url.ToString());
        }
        
        if (!IsPostBack)
        {
            Panel_Add.Visible = false;

            CategoryDAO cate = new CategoryDAO();
            cbbCategoryList.DataSource = cate.ExecuteSelectALL().Tables[0]; ;
            cbbCategoryList.DataBind();

            

            ItemDAO itemcategory = new ItemDAO();
            cbbItemList.DataSource = itemcategory.ExecuteSelectFollowCategoryID(cbbCategoryList.SelectedValue.Trim()).Tables[0];
            cbbItemList.DataBind();

            int itemid = 0;
            int.TryParse(cbbItemList.SelectedValue,out itemid);

            SourceCodeDAO sourcecode = new SourceCodeDAO();
            GridView1.DataSource = sourcecode.ExecuteSelectFollowItemID(itemid).Tables[0];
            GridView1.DataBind();

            //For Add or Edit

            ItemDAO item = new ItemDAO();
            DataTable dt = item.ExecuteSelectALL().Tables[0];

            ItemList_Add.DataSource = dt;
            ItemList_Add.DataBind();

        }
        
        
    }
   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int sourcecodeid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        SourceCodeDAO sourcecode = new SourceCodeDAO();
        DataTable dt = sourcecode.ExecuteSelectFollowSourceCodeID(sourcecodeid).Tables[0];

        ItemList_Add.SelectedValue = dt.Rows[0]["ItemID"].ToString();
        cbbLanguage_Add.SelectedValue = dt.Rows[0]["Language"].ToString().Trim();
        txtSourcePath_Add.Text = dt.Rows[0]["SourcePath"].ToString();
        txtCode_Add.Text = dt.Rows[0]["SourceCode"].ToString();

        if (txtSourcePath_Add.Text.Trim() == "")
        {
            btn_Delete.Enabled = false;
        }
        else
        {
            btn_Delete.Enabled = true;
        }
        Panel_Add.Visible = true;
    }
    
    
    protected void btn_Delete_Click(object sender, EventArgs e)
    {

        try
        {
            //Delteto File from server
            string sFolderPath = Server.MapPath(@"..\SourceCodeFiles");
            string sFileName = GridView1.SelectedRow.Cells[4].Text;
            string filepath = string.Format(@"{0}\{1}", sFolderPath, sFileName);
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            else
            {
                lbl_Message.Text = "File is not exist";

                return;
            }


            // Updata to DB
            int sourcecodeid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

            SourceCodeDAO sourcecode = new SourceCodeDAO();
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("SourceCodeID", sourcecodeid));
            parameters.Add(new SqlParameter("ItemID", ItemList_Add.SelectedValue));
            parameters.Add(new SqlParameter("Language", cbbLanguage_Add.SelectedValue));
            parameters.Add(new SqlParameter("SourceCode", txtCode_Add.Text));
            parameters.Add(new SqlParameter("SourcePath", ""));

            sourcecode.ExecuteUpdate(parameters);

            txtSourcePath_Add.Text = "";
            lbl_Message.Text = "Delete successfully!";
        }
        catch (Exception ex)
        {
            lbl_Message.Text = "Delete failed!";
        }

        cbbItemList_SelectedIndexChanged(null, null);
    }

    protected void reset()
    {
        txtSourcePath_Add.Text = "";
    }
    protected void cbbCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ItemDAO itemcategory = new ItemDAO();
        cbbItemList.DataSource = itemcategory.ExecuteSelectFollowCategoryID(cbbCategoryList.SelectedValue.Trim()).Tables[0];
        cbbItemList.DataBind();

        cbbItemList_SelectedIndexChanged(null, null);

        if (cbbItemList.SelectedValue == "")
        {
            Panel_Add.Visible = false;
        }
        else
        {
            Panel_Add.Visible = true;
        }

    }
    protected void cbbItemList_SelectedIndexChanged(object sender, EventArgs e)
    {
        int itemid = 0;
        int.TryParse(cbbItemList.SelectedValue, out itemid);

        SourceCodeDAO sourcecode = new SourceCodeDAO();
        GridView1.DataSource = sourcecode.ExecuteSelectFollowItemID(itemid).Tables[0];
        GridView1.DataBind();
        
        //Panel_Add.Visible = true;

    }
    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        
        try
        {
            //Upload File to server
            string sFolderPath = Server.MapPath(@"..\SourceCodeFiles");
            HttpPostedFile myFile = FileUpload1.PostedFile;
            string sFileName = ItemList_Add.SelectedValue + "_" + cbbLanguage_Add.SelectedValue + "_" + myFile.FileName;
            myFile.SaveAs(
                string.Format(@"{0}\{1}", sFolderPath, sFileName));
            
            
            // Updata to DB
            int sourcecodeid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

            SourceCodeDAO sourcecode = new SourceCodeDAO();
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("SourceCodeID", sourcecodeid));
            parameters.Add(new SqlParameter("ItemID", ItemList_Add.SelectedValue));
            parameters.Add(new SqlParameter("Language", cbbLanguage_Add.SelectedValue));
            parameters.Add(new SqlParameter("SourceCode", txtCode_Add.Text));
            parameters.Add(new SqlParameter("SourcePath", ItemList_Add.SelectedValue + "_" + cbbLanguage_Add.SelectedValue + "_" + myFile.FileName));

            sourcecode.ExecuteUpdate(parameters);

            lbl_Message.Text = "Upload successfully!";
            txtSourcePath_Add.Text = ItemList_Add.SelectedValue + "_" + cbbLanguage_Add.SelectedValue + "_" + myFile.FileName;

        }
        catch (Exception ex)
        {
            lbl_Message.Text = "Upload failed!";
        }

        cbbItemList_SelectedIndexChanged(null, null);
        
    }
}